package com.example.blogsystem.config;

import com.example.blogsystem.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ResponseBody
@ControllerAdvice
public class ExceptionHandle {
    private Result<Object> result = new Result<>();
    // 第一种方式，捕获所有异常
    @ExceptionHandler
    public Result<Object> handle(Exception e) {
        return result.fail(e.getMessage());
    }


        /*
            第二种写法，通过注解捕获某个具体的异常
            捕获的是空指针异常
            @ExceptionHandler(NullPointerException.class)
            public Result<Object> handle(Exception e) {
                return result.fail(e.getMessage());
            }
         */



}
